﻿var idx = -1;
var fragemets = [];
layui.define(['layer', 'form'], function (exports) {
    var form = layui.form();
    function getSelectionHtml() {
        var html = "";
        if (typeof window.getSelection != "undefined") {
            var sel = window.getSelection();
            if (sel.rangeCount) {
                var container = document.createElement("div");
                for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                    container.appendChild(sel.getRangeAt(i).cloneContents());
                }
                html = container.innerHTML;
            }
        } else if (typeof document.selection != "undefined") {
            if (document.selection.type == "Text") {
                html = document.selection.createRange().htmlText;
            }
        }
        return html;
    }
    function getSelectionText() {
        if (window.getSelection) {
            return window.getSelection().toString();
        } else if (document.selection && document.selection.createRange) {
            return document.selection.createRange().text;
        }
        return '';
    }

    function addFragment(tag, content) {
        fragemets.push({ tag: tag, content: content });
        var item = $('.fragment:not(:visible)').clone();
        item.attr('title', tag);
        item.val(content);
        $("#fragmentList").append(item.show());
    }
    form.on('submit(demo1)', function (data) {
        return false;
    });
    $(".wangEditor-txt").on('keydown', function (e) {
        if ((e.ctrlKey && e.keyCode == 13) && idx == -1) {
            var ui = $(".layer-tpl").clone();
            ui.find('form').addClass('layer-inst');
            ui.find('[name=tag]').empty();
            ui.find('[name=content]').text(getSelectionHtml());
            var html = ui.html();
            delete (ui);
            idx = layer.open({
                title: '设置标签',
                btn: ['确定', '取消'],
                type: 1,
                area: ['700px', 'auto'],
                content: html,
                yes: function () {
                    var ui = $(".layer-inst");
                    if (ui.find('[name=tag]').val() == '') {
                        return;
                    }
                    addFragment(ui.find('[name=tag]').val(), ui.find('[name=content]').text());
                    layer.close(idx);
                    idx = -1;
                },
                btn2: function () {
                    layer.close(idx);
                    idx = -1;
                },
                cancel: function (index, layero) {
                    layer.close(index);
                    idx = -1;
                    return false;
                }
            });
            $('.layui-layer [name=tag]').focus();
            $('.layui-layer').on('keydown', function (e) {
                if (e.keyCode == 13 && idx != -1)
                    $(this).find('.layui-layer-btn0').click();
            });
        }
    });
    exports('article/edit2', {});
});